package com.itheima.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itheima.entity.Banner;
import com.itheima.entity.Movies;
import com.itheima.mapper.BannerMapper;
import com.itheima.service.BannerService;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@RequiredArgsConstructor
public class BannerServiceImpl implements BannerService {

    private final BannerMapper bannerMapper;

    @Override
    public List<Banner> findBannerList() {
        //查询前5条数据
        PageHelper.startPage(1,5);
        List<Banner> list = bannerMapper.findList(null);
        return list;
    }

    @Override
    public PageInfo findByPage(Integer pageNum, Integer pageSize, String title) {
        //1.参数校验
        if (pageNum<=0) {
            pageNum=1;
        }
        if (pageSize<=0) {
            pageSize=5;
        }

        //2.使用pageHelper完成分页查询
        //- PageHelper.startPage(当前页码,每页条数);
        PageHelper.startPage(pageNum,pageSize);

        List<Banner> list = bannerMapper.findList(title);

        //- 使用PageInfo构造器封装list,就可以获取总记录数和分页的一些其他参数
        return new PageInfo(list);
    }

    @Override
    public Banner findById(Integer id) {
        return bannerMapper.findById(id);
    }

    @Override
    public boolean removeById(List<String> list) {
        return bannerMapper.remove(list);
    }

    @Override
    public void add(Movies movie) {
        //copy类信息
        Banner banner = Banner.builder()
                .title(movie.getTitle())
                .url(movie.getUrl())
                .picPath(movie.getPicPath())
                .contentDesc(movie.getContentDesc())
                .orderNo(movie.getOrderNo())
                .build();
        bannerMapper.add(banner);
    }
}
